﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PcBackgroundMgr.Controls;
using OnTheWay.Functions;
using Models;
using System.Collections.ObjectModel;
using Microsoft.Win32;
using System.ComponentModel;
using LocalService;
using System.Threading;

namespace PcBackgroundMgr.Functions
{
    /// <summary>
    /// ExhibitRelateList.xaml 的交互逻辑
    /// </summary>
    public partial class ExhibitRelateList : ExhibitRelateListBase
    {
        public ExhibitRelateList()
        {
            InitializeComponent();
        }
        
        protected override void OnCommandDelete(object param)
        {
            //&& MessageBox.Show("确定要删除["+model.Name+"]吗？","提问",MessageBoxButton.OKCancel) == MessageBoxResult.OK
            Exhibit model = param as Exhibit;
            if (model != null )
            {
                Items.Remove(model);
                if (Items.Count > 0)
                {
                    string ids = Items[0].ID.ToString();
                    for (int i = 1; i < Items.Count; i++)
                    {
                        ids += "," + Items[i].ID.ToString();
                    }
                    this.RelateIDs = ids;
                }
                else
                {
                    this.RelateIDs = "";
                }
            }
        }

        protected override void OnCommandAdd(object obj)
        {
            if (obj is Exhibit)
            {
                this.Items.Add(obj as Exhibit);
                string ids = Items[0].ID.ToString();
                for (int i = 1; i < Items.Count; i++)
                {
                    ids += ","+Items[i].ID.ToString();
                }
                this.RelateIDs = ids;
            }
            else
            {
                ExhibitSelectCtrl fun = new ExhibitSelectCtrl();
                fun.SetOnAdded(x => OnCommandAdd(x));
                fun.OperateMode = FunctionCtrlModelBase.OperateModeEnum.Add;

                FrameworkElement control = this;
                do
                {
                    control = control.Parent as FrameworkElement;
                } while (!(control is FunctionCtrlModelBase) && control!=null);
                if (control is FunctionCtrlModelBase)
                {
                    FunctionCtrlModelBase patent = control as FunctionCtrlModelBase;
                    patent.IFunPanel.FunctionStackPut(fun);
                }
            }
        }

        private void ExhibitItemCtrl_OnDelete(object sender, RoutedEventArgs e)
        {
            OnCommandDelete(e.OriginalSource);
        }

    }

    [DesignTimeVisible(false)]
    public partial class ExhibitRelateListBase : FunctionCtrlList<Exhibit>
    {
        public ExhibitRelateListBase()
            : base(null,typeof(ExhibitSelectCtrl))
        {

        }

        public string RelateIDs
        {
            get { return (string)GetValue(RelateIDsProperty); }
            set 
            { 
                SetValue(RelateIDsProperty, value); 
            }
        }

        // Using a DependencyProperty as the backing store for RelateIDs.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RelateIDsProperty =
            DependencyProperty.Register("RelateIDs", typeof(string),typeof(ExhibitRelateListBase),
            new FrameworkPropertyMetadata(null, OnRelateIDsPropertyChanged));

        public static void OnRelateIDsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ExhibitRelateListBase ctrl = d as ExhibitRelateListBase;
            if (!String.IsNullOrWhiteSpace(e.NewValue as string))
            {
                ctrl.LoadExhibitRelateList(e.NewValue as string);
            }
        }

        private void LoadExhibitRelateList(string value)
        {
            string[] ids = value.Split(',');
            string id = "'"+ids[0] + "'";
            for (int i = 1; i < ids.Length; i++)
            {
                id += ",'" + ids[i] + "'";
            }
            mSqlWhere = " where ID in ("+id+")";
            mDomain = LocalFactory.ExhibitLocal;
            OnCommandRefresh(null);
        }
    }
}
